package com.ddone.util;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author ddone
 * @date 2024/4/4-17:49
 */
public class RabbitMqConnectionUtil {
    // localhost:15672
    private static final String  HOST = "127.0.0.1";
    private static final Integer PORT = 5672;
    private static final String USERNAME="ddone";
    private static final String PWD="abc123";
    private static final String VIRTUAL_HOST = "rabbit-demo";

    public static Connection getConnection() throws IOException, TimeoutException {
        // 定义连接工程
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(HOST);
        factory.setPort(PORT);

        factory.setVirtualHost(VIRTUAL_HOST);
        factory.setUsername(USERNAME);
        factory.setPassword(PWD);

        // 通过工程获取连接
        return factory.newConnection();
    }
}
